<div class="loading-container"><div class="loader"></div></div>

<div class="loading-waiter">
	<div class="loader">
		<div class="texting">请稍后...</div>
		<div class="spinner">
		  <div class="rect1"></div>
		  <div class="rect2"></div>
		  <div class="rect3"></div>
		  <div class="rect4"></div>
		  <div class="rect5"></div>
		</div>
	</div>
</div>

<div class="wrapper wrapper-shadow wrapper-list"> 
	<!-- list data wrapper -->
	<div class="result-wrapper no-padding">
   	<div class="result-body">
   		<div class="tabs">
      	<ul class="nav nav-tabs widget no-margin-bottom">
        	<li class="active"> 
         		<a href="#tab-content" data-toggle="tab"> 
         			<span class="menu-icon"><i class="fa fa-bar-chart"></i></span> 销售分析表
           	</a>
         	</li>
        </ul>
        <div class="tab-content no-border xs-padding">
					<div class="tab-pane active" id="tab-content" style="padding:10px 0;">
           	<div class="search-panel" style="margin-bottom: 10px;">
           		<form class="form-inline" id="search-form" action="/miwawa/{dbname}/common/procedure/call" method="post">
           			<div class="form-group">
							    <label for="from">统计时间：</label>
							    <div class="input-group date date-selector">
										<input id="from" type="text" class="form-control" placeholder="开始时间"> 
										<span class="input-group-addon">
											<span class="fa fa-calendar"></span>
										</span>
									</div>
									<span>-</span>
									<div class="input-group date date-selector">
										<input id="over" type="text" class="form-control" placeholder="结束时间"> 
										<span class="input-group-addon">
											<span class="fa fa-calendar"></span>
										</span>
									</div>
							  </div>
							  
							  <div class="vertical-space space-5"></div>
							  
							  <div class="form-group">
							    <label for="from">市场渠道：</label>
							    <div class="input-group">
										<select id="market" multiple class="chosen-select"  data-url="/miwawa/default/common/select.json" data-pcode="c_market_type" data-entity="com.app.miwawa.api.system.entity.config.Codec">
										</select>
										<span class="input-group-btn">
											<button type="button" class="btn btn-addon" onclick="handlerClear('#market');"><i class="fa fa-trash"></i></button>
										</span>
									</div>
									<button type="button" class="btn btn-warning" onclick="handlerReset();">重置</button>
							  </div>
							  
							  <div class="vertical-space space-5"></div>
							  
							  <div class="form-group">
							    <label for="from">信息来源：</label>
							    <div class="input-group">
								    <select id="source" multiple class="chosen-select" style="width: 399px;" data-url="/miwawa/default/common/select.json" data-pcode="c_source_type" data-entity="com.app.miwawa.api.system.entity.config.Codec">
										</select>
										<span class="input-group-btn">
											<button type="button" class="btn btn-addon" onclick="handlerClear('#source');"><i class="fa fa-trash"></i></button>
										</span>
									</div>
									<button type="button" class="btn btn-primary" onclick="handlerQuery();">统计</button>
									<button type="button" class="btn btn-success" onclick="handlerExport();">导出</button>
							  </div>
           		</form>
           	</div>
           	<div id="table-a1">
           		<table class="table table-bordered table-hover">
	           		<thead>
	           			<tr>
	           				<th rowspan="2" class="table-th-center">顾问</th>
	           				<th rowspan="2" class="table-th-center">总非会员</th>
	           				<th colspan="2" class="table-th-center">有效名单</th>
	           				<th colspan="2" class="table-th-center">流失名单</th>
	           				<th colspan="3" class="table-th-center">跟进数</th>
	           				<th colspan="2" class="table-th-center">诺访数</th>
	           				<th colspan="2" class="table-th-center">到店数</th>
	           				<th colspan="3" class="table-th-center">签约数</th>
	           			</tr>
	           			<tr>
	           				<th class="table-th-center">人数</th>
	           				<th class="table-th-center">占比</th>
	           				<th class="table-th-center">人数</th>
	           				<th class="table-th-center">占比</th>
	           				<th class="table-th-center">人次</th>
	           				<th class="table-th-center">人数</th>
	           				<th class="table-th-center">占比</th>
	           				<th class="table-th-center">人数</th>
	           				<th class="table-th-center">占比</th>
	           				<th class="table-th-center">人数</th>
	           				<th class="table-th-center">占比</th>
	           				<th class="table-th-center">人数</th>
	           				<th class="table-th-center">占比</th>
	           				<th class="table-th-center">金额</th>
	           			</tr>
	           		</thead>
	           		
	           		<tbody id="tabledata-a1"></tbody>
	           		
	           		<tfoot id="tablesum-a1">
	           			<tr>
	           				<th class="table-th-center">合计</th>
	           				<th class="table-th-center sum-totals"></th>
	           				<th class="table-th-center sum-validCount"></th>
	           				<th class="table-th-right  pec-validCount"></th>
	           				<th class="table-th-center sum-lostedCount"></th>
	           				<th class="table-th-right  pec-lostedCount"></th>
	           				<th class="table-th-center sum-followNumber"></th>
	           				<th class="table-th-center sum-followCount"></th>
	           				<th class="table-th-right  pec-followCount"></th>
	           				<th class="table-th-center sum-promiseCount"></th>
	           				<th class="table-th-right  pec-promiseCount"></th>
	           				<th class="table-th-center sum-commingCount"></th>
	           				<th class="table-th-right  pec-commingCount"></th>
	           				<th class="table-th-center sum-membersCount"></th>
	           				<th class="table-th-right  pec-membersCount"></th>
	           				<th class="table-th-center sum-membersPrice"></th>
	           			</tr>           			
	           		</tfoot>
	           	</table>
           	</div>
        	</div>
      	</div>
    	</div>
   	</div>
	</div>

	<!-- form editor wrapper -->
	<div class="editor-wrapper">
		
	</div>
</div>
<!-- PAGE CONTENT END =============================================== -->

<form id="export-form" action="/miwawa/{dbname}/common/procedure/export" method="post">
	<input type="hidden" name="name" value="销售分析月表"/>
</form>

<!-- Javascript ===================================================== -->
<script src="/miwawa/resources/lib/miwawa/1.0.2/js/core/requirejs/require.js"></script>

<script id="template-report-a1" type="text/x-handlebars-template">
	{{#each this}}
		<tr>
			<td id="{{userid}}">{{username}}</td>
			<td align="center" class="count-totals">{{totals}}</td>
			<td align="center" class="count-validCount">{{validCount}}</td>
			<td align="right"  class="percent-validCount">{{_percent totals validCount}}</td>
			<td align="center" class="count-lostedCount">{{lostedCount}}</td>
			<td align="right"  class="percent-lostedCount">{{_percent totals lostedCount}}</td>
			<td align="center" class="count-followNumber">{{followNumber}}</td>
			<td align="center" class="count-followCount">{{followCount}}</td>
			<td align="right"  class="percent-followCount">{{_percent totals followCount}}</td>
			<td align="center" class="count-promiseCount">{{promiseCount}}</td>
			<td align="right"  class="percent-promiseCount">{{_percent totals promiseCount}}</td>
			<td align="center" class="count-commingCount">{{commingCount}}</td>
			<td align="right"  class="percent-commingCount">{{_percent totals commingCount}}</td>
			<td align="center" class="count-membersCount">{{membersCount}}</td>
			<td align="right"  class="percent-membersCount">{{_percent totals membersCount}}</td>
			<td align="right"  class="count-membersPrice">{{_price membersPrice}}</td>
		</tr>
	{{/each}}
</script>

<script type="text/javascript">
<!--
	jQuery(function(){
		require(['jquery.plugin.form'],function(form){
			form.datepicker(".date-selector",{defaultDate:null});
			form.tooltip("[data-toggle='tooltip']");
			
			form.chosen("#market").select("#market");
			form.chosen("#source").select("#source");
		});
		
		initData();
	});
	
	// 清空多选下拉框
	function handlerClear(elem){
		jQuery(".search-choice .search-choice-close", elem+"_chosen").click();
	}
	
	// 重置查询条件
	function handlerReset(){
		jQuery("#search-form").resetForm();
		jQuery(".search-choice .search-choice-close").click();
	}
	
	// 导出
	function handlerExport(){
		var action = jQuery("#export-form").attr("action").replace("{dbname}", dbname);
		jQuery("#export-form").attr("action", action);
		jQuery(":hidden[name='params[html]']","#export-form").remove();
		jQuery("#export-form").append('<input type="hidden" name="params[html]"/>');
		jQuery(":hidden[name='params[html]']","#export-form").val(jQuery("#table-a1").html());
		jQuery("#export-form")[0].submit();
	}
	
	// 统计
	function handlerQuery(){
		jQuery("#search-form").ajaxSubmit({
			url: jQuery("#search-form").attr("action").replace("{dbname}", dbname),
			data: {
				"name":"pro_report_sales_month",
				"params":{
					"_from":jQuery("#from").val(),
					"_over":jQuery("#over").val(),
					"_market":jQuery.utils.toSQLEscapeValue(jQuery("#market").val()),
					"_source":jQuery.utils.toSQLEscapeValue(jQuery("#source").val())
				}
			},
			dataType: "json",
			beforeSubmit: function($arr, $form, $options) {
				jQuery.utils.loading("正在准备统计数据，请稍后...", "show");
				return true;
			},
			success: function(data){
				createData(data);
				jQuery.utils.loading(null, "hide");
			}
		});
	}
	
	// 初始化数据
	function initData(){
		jQuery.ajaxsend({
			url:"/miwawa/{dbname}/common/procedure/call".replace("{dbname}",dbname),
			data:{
				"name":"pro_report_sales_month",
				"params":{
					"_from":null,
					"_over":null,
					"_market":null,
					"_source":null
				}
			},
			success:function(data){
				createData(data);
			}
		});
	}
	

	// 填充合计行
	function handlerTotals(){
		var total = 0;
		jQuery(".count-totals").each(function(i,n){
			total += new Number(n.innerText);
		});
		jQuery(".sum-totals").text(total);
		
		var valid = 0;
		jQuery(".count-validCount").each(function(i,n){
			valid += new Number(n.innerText);
		});
		jQuery(".sum-validCount").text(valid);
		jQuery(".pec-validCount").text(total == 0 ? '0%' : (valid/total*100).toFixed(2)+'%');
		
		var losted = 0;
		jQuery(".count-lostedCount").each(function(i,n){
			losted += new Number(n.innerText);
		});
		jQuery(".sum-lostedCount").text(losted);
		jQuery(".pec-lostedCount").text(total == 0 ? '0%' : (losted/total*100).toFixed(2)+'%');
		
		var follow = 0;
		jQuery(".count-followNumber").each(function(i,n){
			follow += new Number(n.innerText);
		});
		jQuery(".sum-followNumber").text(follow);
		
		follow = 0;
		jQuery(".count-followCount").each(function(i,n){
			follow += new Number(n.innerText);
		});
		jQuery(".sum-followCount").text(follow);
		jQuery(".pec-followCount").text(total == 0 ? '0%' : (follow/total*100).toFixed(2)+'%');
		
		var promise = 0;
		jQuery(".count-promiseCount").each(function(i,n){
			promise += new Number(n.innerText);
		});
		jQuery(".sum-promiseCount").text(promise);
		jQuery(".pec-promiseCount").text(total == 0 ? '0%' : (promise/total*100).toFixed(2)+'%');
		
		var comming = 0;
		jQuery(".count-commingCount").each(function(i,n){
			comming += new Number(n.innerText);
		});
		jQuery(".sum-commingCount").text(comming);
		jQuery(".pec-commingCount").text(total == 0 ? '0%' : (comming/total*100).toFixed(2)+'%');
		
		var members = 0;
		jQuery(".count-membersCount").each(function(i,n){
			members += new Number(n.innerText);
		});
		jQuery(".sum-membersCount").text(members);
		jQuery(".pec-membersCount").text(total == 0 ? '0%' : (members/total*100).toFixed(2)+'%');
		
		members = 0;
		jQuery(".count-membersPrice").each(function(i,n){
			members += new Number(n.innerText);
		});
		jQuery(".sum-membersPrice").text(members.toFixed(2));
	}
	
	function createData(data){
		require(['handlebars'],function(Handlebars){
			var compile = Handlebars.compile(jQuery("#template-report-a1").html());
			
			Handlebars.registerHelper("_percent", function(totals, count){
				return totals == 0 ? '0%' : (count/totals*100).toFixed(2) + '%';
			});
			
			Handlebars.registerHelper("_price", function(price){
				return price.toFixed(2);
			});
			
			jQuery("#tabledata-a1").html(data && data.rows.length > 0 ? compile(data.rows) : "<tr><td colspan='16' align='center'>无数据</td></tr>");
			
			handlerTotals();
			
			handlerSuccess();
		});
	}
//-->
</script>
